package commands;

import java.util.Arrays;
import java.util.List;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;

import persistence.IPermissiable;

public class Commandsethome implements ICommand, IPermissiable {
	CCommandHandler handler;
	final static String CONST_COMMAND_NAME = "sethome";
	final static String CONST_PERM_SETHOME = "bcommand.sethome";
	final static List<Permission> CONST_PERM_LIST = Arrays
			.asList(new Permission(CONST_PERM_SETHOME));

	public Commandsethome(CCommandHandler parent) {
		handler = parent;
	}

	@Override
	public boolean checkSender(CommandSender sender) {
		for (Permission p : CONST_PERM_LIST) {
			if (!sender.hasPermission(p))
				return false;
		}
		return true;
	}

	@Override
	public List<Permission> getPermList() {
		return CONST_PERM_LIST;
	}

	@Override
	public String getCommandName() {
		return CONST_COMMAND_NAME;
	}

	@Override
	public boolean isCommandAccepted(String cmd) {
		return CONST_COMMAND_NAME.equals(cmd);
	}

	@Override
	public boolean exec(CommandSender sender, String[] args) {
		Player p = null;
		if (sender instanceof Player) {
			p = (Player) sender;
		} else {
			if (args.length == 1) {
				for (Player l : handler.parent.getParent().getServer()
						.getOnlinePlayers()) {
					if (l.getName().equals(args[1])) {
						p = l;
					} else {
						return false;
					}
				}
			} else {
				return false;
			}
		}

		((Commandhome) handler.getCmdByName(Commandhome.CONST_COMMAND_NAME))
				.setHome(p, p.getLocation());
		return true;
	}

}
